<?php


namespace app\api\services;

use app\admin\model\HtnsOrder;
use app\admin\traits\HtnsTrait;
use think\Exception;
use think\facade\Cache;

/**
 * Class HtnsService
 * @package app\api\services
 * @desc 海淘系统API相关服务
 */
class HtnsApiService
{
    use HtnsTrait;

    /**
     * @desc 查询订单物流信息
     * @param int $id 订单id
     * @param int $orderStatus 订单状态
     * @param array $header 授权头文件
     * @return array
     */
    public function getExpress($id , $orderStatus = 0)
    {
        try {
            $rest   = [];
            $domain = sysconfig('other', 'domain');// 域名
            $rest['express'] = [];
            $rest['expressStatu'] = '';
            $rest['shStatu'] = '';
            $order = HtnsOrder::find($id);
            if($order['delivery_id']){
                $kdurls          = $domain.'/api/kdi?no=' . $order['delivery_id'].'&type='.$order['delivery_code'];
                $expressData    = $this->httpCurlGet($kdurls, 1);
                $expressDataArr = json_decode($expressData, true);
                if ($expressDataArr['status'] == 200) {
                    $deliveryData = $expressDataArr['data'];
                    if($deliveryData['list']){
                        $rest['expressStatu'] = '已发货';
                    }
                    if($deliveryData['issign']){
                        $rest['expressStatu'] = '已签收';
                    }
                    $rest['express'] = $deliveryData['list'];
                }else{
                    return $this->returnArr(0, 'HtnsService/getExpress查询失败,'.$expressDataArr['msg'], $rest);
                }
                // 快递已签收,订单状态未签收
                if($rest['expressStatu'] == '已签收' && $orderStatus!=2){
                    // 调用接口收货
                    $shurls          = $domain.'/api/jizhan/take/' . $id;
                    $shData    = $this->httpCurlGet($shurls, 1);
                    $shDataArr = json_decode($shData, true);
//            halt($shurls,$shDataArr);
                    if ($shDataArr['status'] == 200) {
                        $rest['shStatu'] = 'POST方法收货成功';
                    }
                }
            }
            return $this->returnArr(1, 'HtnsService/getExpress查询物流成功', $rest);
        } catch (Exception $e) {
            return $this->returnArr(0, 'HtnsService/getExpress服务异常,' . $e->getMessage());
        }
    }

    /**
     * @desc 订单签收
     * @param int $id 订单id
     * @param array $header 授权头文件
     * @return array
     */
    public function takeOrder($id)
    {
        try {
            $rest   = [];
            $domain = sysconfig('other', 'domain');// 域名
            // 调用接口收货
            $shurls          = $domain.'/api/jizhan/take/' . $id;
            $shData    = $this->httpCurlPost($shurls);
            $shDataArr = json_decode($shData, true);
            if ($shDataArr['status'] == 200) {
                $rest['shStatu'] = 'POST方法收货成功';
            }else{
                return $this->returnArr(0, 'HtnsService/takeOrder订单签收失败,'.$shDataArr['msg'], $shDataArr);
            }
            return $this->returnArr(1, 'HtnsService/takeOrder订单签收成功', $rest);
        } catch (Exception $e) {
            return $this->returnArr(0, 'HtnsService/takeOrder订单签收服务异常,' . $e->getMessage());
        }
    }
    /**
     * @desc 全国快递查询
     * @param  string $no 快递单号
     * @param string $type 快递公司简称
     */
    public function getNationalExpress($no , $type = '')
    {
        try {
            $rest   = [];
            $domain = sysconfig('other', 'domain');// 域名
            // 调用接口收货
            $shurls = $domain . '/api/kdi?no=' . $no.'&type='.$type;
            $shData    = $this->httpCurlGet($shurls, 1);
            $shDataArr = json_decode($shData, true);
            if ($shDataArr['status'] == 200) {
                $rest['issign'] = $shDataArr['data']['issign'];
                $rest['express'] = $shDataArr['data']['list'];
            }else{
                return $this->returnArr(0, 'HtnsService/getNationalExpress全国快递查询失败,'.$shDataArr['msg'], $shDataArr);
            }
            return $this->returnArr(1, 'HtnsService/getNationalExpress全国快递查询成功', $rest);
        } catch (Exception $e) {
            return $this->returnArr(0, 'HtnsService/getNationalExpress全国快递查询服务异常,' . $e->getMessage());
        }
    }

    /**
     * @desc 仅退款-立即退款
     * @param int $id 退款表id
     * @param string $order_id 订单号
     * @param string $refund_price 退款金额
     * @return array
     */
    public function refundAgree($id , $order_id, $refund_price)
    {
        try {
            $rest   = [];
            $domain = sysconfig('other', 'domain');// 域名
            // 调用接口收货
            $shurls = $domain . '/api/jizhan/refund/' . $id;
            $dataArr['order_id'] = $order_id;
            $dataArr['refund_price'] = $refund_price;
//            $data = json_encode($dataArr);
            $shData    = $this->httpCurlPost($shurls, $dataArr);
            $shDataArr = json_decode($shData, true);
//            halt($shDataArr);
            if ($shDataArr['status'] == 200) {
                $rest['shStatu'] = 'POST方法-仅退款-立即退款成功';
            }else{
                return $this->returnArr(0, 'HtnsService/refundAgree仅退款-立即退款失败,' .$shDataArr['msg']);
            }
            return $this->returnArr(1, 'HtnsService/refundAgree仅退款-立即退款成功', $rest);
        } catch (Exception $e) {
            return $this->returnArr(0, 'HtnsService/refundAgree仅退款-立即退款服务异常,' . $e->getMessage());
        }
    }

    /**
     * @desc 仅退款-不同意退款
     * @param int $id 退款表id
     * @param string $order_id 订单号
     * @param string $refuse_reason 拒绝原因
     * @return array
     */
    public function refundNoAgree($id , $order_id, $refuse_reason)
    {
        try {
            $rest   = [];
            $domain = sysconfig('other', 'domain');// 域名
            $shurls = $domain . '/api/jizhan/no_refund/' . $id;

            $dataArr['order_id'] = $order_id;
            $dataArr['refund_reason'] = $refuse_reason;
            $shData    = $this->httpCurlPost($shurls, $dataArr);
            $shDataArr = json_decode($shData, true);
//            halt('refundNoAgree',$header,$shurls,$dataArr,$shData);
            if ($shDataArr['status'] == 200) {
                $rest['shStatu'] = 'POST方法-仅退款-不同意退款成功';
            }else{
                return $this->returnArr(0, 'HtnsService/refundNoAgree仅退款-不同意退款失败,' .$shDataArr['msg']);
            }
            return $this->returnArr(1, 'HtnsService/refundNoAgree仅退款-不同意退款成功', $rest);
        } catch (Exception $e) {
            return $this->returnArr(0, 'HtnsService/refundNoAgree仅退款-不同意退款服务异常,' . $e->getMessage());
        }
    }

    /**
     * @desc 同意-退货退款
     * @param int $id 退款表id
     * @return array
     */
    public function refundAgreeProduct($id)
    {
        try {
            $rest   = [];
            $domain = sysconfig('other', 'domain');// 域名
            // 调用接口收货
            $shurls = $domain . '/api/jizhan/agree/' . $id;
            $shData    = $this->httpCurlGet($shurls , 1);
            $shDataArr = json_decode($shData, true);
            if ($shDataArr['status'] == 200) {
                $rest['shStatu'] = 'GET方法-仅退款-立即退款成功';
            }else{
                return $this->returnArr(0, 'HtnsService/refundAgreeProduct同意-退货退款失败,' .$shDataArr['msg']);
            }
            return $this->returnArr(1, 'HtnsService/refundAgreeProduct同意-退货退款成功', $rest);
        } catch (Exception $e) {
            return $this->returnArr(0, 'HtnsService/refundAgreeProduct仅退款-立即退款服务异常,' . $e->getMessage());
        }
    }

    /**
     * @desc 订单发送货
     * @param int $id 订单表id
     * @param string $delivery_name 快递公司名称
     * @param string $delivery_id 快递单号
     * @param string $delivery_code 快递公司编码
     * @param string $express_record_type 发货记录类型
     * @return array
     */
    public function updateDelivery($id , $delivery_name , $delivery_id , $delivery_code , $express_record_type = 1)
    {
        try {
            $rest   = [];
            $domain = sysconfig('other', 'domain');// 域名
            // 调用接口收货
            $shurls = $domain . '/api/jizhan/delivery/' . $id;
            $data['delivery_name'] = $delivery_name;
            $data['delivery_id'] = $delivery_id;
            $data['delivery_code'] = $delivery_code;
            $data['express_record_type'] = $express_record_type;
            $shData    = $this->httpCurlPost($shurls , $data);
            $shDataArr = json_decode($shData, true);
//            var_dump($shDataArr);die;
            if ($shDataArr['status'] == 200) {
                $rest['shStatu'] = 'POST方法-订单发送货成功';
            }else{
                return $this->returnArr(0, 'HtnsApiService/updateDelivery订单发送货失败,' .$shDataArr['msg'],$shDataArr);
            }
            return $this->returnArr(1, 'HtnsApiService/updateDelivery订单发送货成功', $rest);
        } catch (Exception $e) {
            return $this->returnArr(0, 'HtnsApiService/updateDelivery订单发送货服务异常,' . $e->getMessage());
        }
    }
}